//
//  ShopAddressView.m
//  JewelryStore
//
//  Created by apple on 2017/1/9.
//  Copyright © 2017年 com. All rights reserved.
//

#import "ShopAddressView.h"

#define HEAD_HEIGHT SCREEN_WIDTH/7
#define SHOPADDRESS_CELL @"shopAddressCell"
@interface ShopAddressView ()<UITableViewDataSource, UITableViewDelegate, CAAnimationDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIButton *bottomView;
@property (nonatomic, strong) UIView *backView;

@end
@implementation ShopAddressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self addSubview:self.maskView];
    }
    return self;
}
- (void)setModel:(NSArray *)model
{
    _model = model;
    
    if (model.count == 0)
    {
        [self addSubview:self.pickerView];
    }else
    {
        [self addSubview:self.backView];
    }

}

- (void)tapClick
{
    [self hideAndRemoveAddressView];
}

#pragma mark - public

- (void)showAddressView
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    CATransition *transiton = [CATransition animation];
    transiton.type = kCATransitionPush;
    transiton.subtype = kCATransitionFromTop;
    [transiton setFillMode:kCAFillModeBoth];
    [transiton setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.backView.layer addAnimation:transiton forKey:kCATransition];
}

#pragma mark - private

- (void)hideAndRemoveAddressView
{
    CATransition *transiton = [CATransition animation];
    transiton.type = kCATransitionReveal;
    transiton.subtype = kCATransitionFromBottom;
    transiton.delegate = self;
    [transiton setFillMode:kCAFillModeBackwards];
    [transiton setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [self.backView.layer addAnimation:transiton forKey:kCATransition];

}

- (void)buttonClick
{
    [self.backView addSubview:self.pickerView];
}

#pragma mark - lazy

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEAD_HEIGHT, SCREEN_WIDTH, CGRectGetHeight(self.frame)/2 - 2*HEAD_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        //注册cell
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SHOPADDRESS_CELL];
    }
    return _tableView;
}

- (UIPickerView *)pickerView
{
    if (!_pickerView) {
        
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGRectGetHeight(self.frame)/2 - HEAD_HEIGHT)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        
    }
    return _pickerView;
}

- (UIView *)maskView
{
    if (!_maskView) {
        
        _maskView = [[UIView alloc] initWithFrame:self.frame];
        
        _maskView.backgroundColor = [UIColor grayColor];
        
        _maskView.alpha = 0.6;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        
        [_maskView addGestureRecognizer:tap];

    }
    return _maskView;
}

- (UIView *)headView
{
    if (!_headView) {
        
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEAD_HEIGHT)];
        
        _headView.backgroundColor = kWhite;
        
        UILabel *lab = [[UILabel alloc] init];
        
        lab.text = @"配送至";
        
        lab.textColor = RGB(155, 155, 155);
        
        [_headView addSubview:lab];
        
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(_headView);
        }];
    }
    return _headView;
}

- (UIButton *)bottomView
{
    if (!_bottomView) {
        
        _bottomView = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _bottomView.backgroundColor = [UIColor redColor];
        
        _bottomView.frame = CGRectMake(0, CGRectGetHeight(_backView.frame) - HEAD_HEIGHT, CGRectGetWidth(self.frame), HEAD_HEIGHT);
        
        [_bottomView setTitle:@"选择其他地址" forState:UIControlStateNormal];
        
        [_bottomView addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _bottomView;
}

- (UIView *)backView
{
    if (!_backView) {
        
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)/2, SCREEN_WIDTH, CGRectGetHeight(self.frame)/2)];
        
        [_backView addSubview:self.headView];
        
        [_backView addSubview:self.tableView];
        
        [_backView addSubview:self.bottomView];
        
    }
    return _backView;
}

#pragma mark - tableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SHOPADDRESS_CELL forIndexPath:indexPath];
    cell.fd_isTemplateLayoutCell = NO;
    cell.textLabel.text = @"广东省深圳市罗湖区田贝四路翠竹酒店5楼广东省深圳市罗湖区田贝四路翠竹酒店5楼广东省深圳市罗湖区田贝四路翠竹酒店5楼广东省深圳市罗湖区田贝四路翠竹酒店5楼";
    
    cell.textLabel.numberOfLines = 0;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return [tableView fd_heightForCellWithIdentifier:SHOPADDRESS_CELL cacheByIndexPath:indexPath configuration:^(id cell) {
       UITableViewCell *tcell = cell;
       
       tcell.textLabel.text = @"广东省深圳市罗湖区田贝四路翠竹酒店5楼广东省深圳市罗湖区田贝四路翠竹酒店5楼广东省深圳市罗湖区田贝四路翠竹酒店5楼广东省深圳市罗湖区田贝四路翠竹酒店5楼";
       
       tcell.textLabel.numberOfLines = 0;
   }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TT_Log(@"click");
    //传数据
    self.update(@"一大批水军正在袭来");
    
    //移除
    [self hideAndRemoveAddressView];
}

#pragma mark - CAAnimation delegate

- (void)animationDidStart:(CAAnimation *)anim
{
    self.backView.alpha = 0;
    self.maskView.alpha = 0;

}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [self removeFromSuperview];
}

#pragma maek - picker view

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 10;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return @"1111";
}
@end
